package com.xinyue.dz.dao.impl;

import com.xinyue.dz.bean.Group;
import com.xinyue.dz.dao.GroupDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class GroupDaoImpl implements GroupDao {
	@Autowired
    private SessionFactory factory;
    
	//默认方法
	@Override
	public void save(Group entity) {
		factory.getCurrentSession().save(entity);
	}
	
	@Override
	public Integer deleteById(int groupId) {
		Query q = factory.getCurrentSession().createQuery("delete Group where groupId=?");
		q.setParameter(0, groupId);
		return q.executeUpdate();
	}
	
	@Override
	public void update(Group entity) {
		factory.getCurrentSession().update(entity);
	}
	
	@Override
	public void saveOrUpdate(Group entity) {
		factory.getCurrentSession().saveOrUpdate(entity);
	}
	
	@Override
	public Group queryById(int groupId) {
		Query q = factory.getCurrentSession().createQuery("from Group where groupId=?");
		q.setParameter(0, groupId);
		return (Group) q.uniqueResult();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Group> queryAll(int begin, int count) {
		Query q = factory.getCurrentSession().createQuery("from Group");
		q.setFirstResult(begin);
		q.setMaxResults(count);
		return q.list();
	}
	
	@Override
	public Long queryCount() {
		return (Long) factory.getCurrentSession().createQuery("select count(*) from Group").uniqueResult();
	}
}